﻿
using System;

namespace Mockery.Core
{
    /// <summary>
    /// The state of the mockery by action type
    /// </summary>
    public class ActiveMockeryState : IActiveMockeryState
    {
        /// <summary>
        /// The current state of the mockery
        /// </summary>
        public MockeryState State { get; private set; }

        private readonly IRepositoryStateHandler _respositoryStateHandler;

        /// <summary>
        /// standard ctor
        /// </summary>
        public ActiveMockeryState(IRepositoryStateHandler repositoryStateHandler)
        {
            _respositoryStateHandler = repositoryStateHandler;
            State = MockeryState.Setup;
        }

        /// <summary>
        /// mechanism to change the state according to mockery state-change rules
        /// </summary>
        /// <param name="mockeryState">target state</param>
        public void SetState( MockeryState mockeryState)
        {
            if (State > mockeryState)
                throw new MockeryOrderException("Test structure is out of order. Must be Arrange..Act..Assert");

            if (State != mockeryState)
                _respositoryStateHandler.SetState(mockeryState);

            State = mockeryState;
        }
    }
}
